package com.example.e_book.DBUtil;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "DBebook";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_USERS = "users";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_PASSWORD = "password";

    private static final String TABLE_CREATE1 =
            "CREATE TABLE " + TABLE_USERS + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_USERNAME + " TEXT, " +
                    COLUMN_PASSWORD + " TEXT" +
                    ")";

    public static final String TABLE_BOOKS = "books";
    public static final String COLUMN_BOOK_ID = "book_id";
    public static final String COLUMN_BOOK_TITLE = "title";
    public static final String COLUMN_BOOK_AUTHOR = "author";
    public static final String COLUMN_CATEGORY = "category"; // 添加类别列的常量
    public static final String COLUMN_BOOK_MAIN_CONTENT = "content_path";
    public static final String COLUMN_BOOK_COVER_IMAGE = "cover_image_path";

    private static final String TABLE_CREATE2 =
            "CREATE TABLE " + TABLE_BOOKS + " (" +
                    COLUMN_BOOK_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_BOOK_TITLE + " TEXT, " +
                    COLUMN_BOOK_AUTHOR + " TEXT, " +
                    COLUMN_CATEGORY + " TEXT, " +
                    COLUMN_BOOK_MAIN_CONTENT + " TEXT," +
                    COLUMN_BOOK_COVER_IMAGE + " TEXT" +

                    ")";

    public static final String TABLE_BOOKSHELF = "bookshelf";
    public static final String COLUMN_BOOKSHELF_BOOK_ID = "book_id";
    public static final String COLUMN_BOOKSHELF_USER_ID = "user_id";

    private static final String TABLE_CREATE3 =
            "CREATE TABLE " + TABLE_BOOKSHELF + " (" +
                    COLUMN_BOOKSHELF_BOOK_ID + " INTEGER, " +
                    COLUMN_BOOKSHELF_USER_ID + " INTEGER, " +
                    "FOREIGN KEY (" + COLUMN_BOOKSHELF_BOOK_ID + ") REFERENCES " + TABLE_BOOKS + "(" + COLUMN_BOOK_ID + "), " +
                    "FOREIGN KEY (" + COLUMN_BOOKSHELF_USER_ID + ") REFERENCES " + TABLE_USERS + "(" + COLUMN_ID + "), " +
                    "PRIMARY KEY (" + COLUMN_BOOKSHELF_BOOK_ID + ", " + COLUMN_BOOKSHELF_USER_ID + ")" +
                    ")";

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE1);
        db.execSQL(TABLE_CREATE2);
        db.execSQL(TABLE_CREATE3);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOKS);
        onCreate(db);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
        onCreate(db);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOKSHELF);
        onCreate(db);
    }
}
